package com.jiazhong.boot.mvc.controller;

import com.jiazhong.boot.commons.bean.Users;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/fourth")
public class FourthController {


    @RequestMapping("/a")
    public void a(HttpServletResponse response) throws IOException {
        log.info("用户访问了FourthController的a方法");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("Hello,world!");
    }

    @RequestMapping("/b")
    @ResponseBody  // 告诉服务器，这里的返回结果不再是页面跳转，而是数据的输出
    public String b() {
        log.info("用户访问了FourthController的b方法");
        return "Hello,world!";
    }

    @RequestMapping(value = "/c", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public Users c() {
        log.info("用户访问了FourthController的c方法");
        Users users = new Users();
        users.setId(1);
        users.setName("张三");
        users.setAge(29);
        users.setGender('男');
        return users;
    }

    @RequestMapping("/d")
    @ResponseBody
    public List<Users> d() {
        log.info("用户访问了FourthController的d方法");
        Users u1 = new Users(1, "张三", 23, '男');
        Users u2 = new Users(2, "李四", 21, '女');
        Users u3 = new Users(3, "王五", 26, '男');
        Users u4 = new Users(4, "马六", 25, '男');
        List<Users> list = Arrays.asList(u1, u2, u3, u4);
        return list;
    }
}
